package org.dfl.plugin.editor.actions;

import org.dfl.messages.Messages;
import org.dfl.plugin.DflPlugin;
import org.dfl.plugin.editor.commands.AutoAlignCommand;
import org.dfl.plugin.editor.commands.CancelCommandRuntimeException;
import org.dfl.plugin.editor.model.DflDiagram;
import org.eclipse.ui.IWorkbenchPart;

/**
 * 
 * Auto-aligns the diagram vertically using graphviz package.
 * 
 * @author Jacek Sroka
 * 
 */

public class AlignVerticallyAction extends org.eclipse.gef.ui.actions.SelectionAction {
	public static final String ACTION_ID = "AutoAlignVert";
	public static final String ICON_NAME = "icons/eclipse/seq_sc_obj2.gif";

	private DflDiagram diagram;

	public AlignVerticallyAction(IWorkbenchPart part, DflDiagram diagram) {
		super(part);
		this.diagram = diagram;
		setId(ACTION_ID);
		setImageDescriptor(DflPlugin.getImageDescriptor(ICON_NAME));
		setHoverImageDescriptor(getImageDescriptor());
		setText(Messages.Align_vertically_name);
		setToolTipText(Messages.Align_vertically_tooltip);
	}

	protected boolean calculateEnabled() {
		if(diagram == null || !diagram.isEditable()) 
			return false;
		if (diagram.isRunnning())
			return false;
		return diagram.canBeAligned();
	}

	public void run() {
		try{
			execute(new AutoAlignCommand(diagram,true));
		}catch(CancelCommandRuntimeException ee){}
	}
}
